/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package lineage.database.tables;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Map;

import lineage.database.L1AbstractDatabase;
import lineage.database.tables.bean.L1Maps;
import lineage.util.PerformanceTimer;
import lineage.util.Util;
import lineage.util.collections.Maps;

/**
 * 地图.
 * 
 * @version 2014年4月5日下午12:36:40
 * @author jrwz
 */
public final class MapsTable extends L1AbstractDatabase {
    private static final Map<Integer, L1Maps> list = Maps.newHashMap();

    /**
     * 静态初始化器，由JVM来保证线程安全.
     */
    private static class Holder {
        static MapsTable instance = new MapsTable();
    }

    /**
     * 取得该类的实例.
     */
    public static MapsTable getInstance() {
        return Holder.instance;
    }

    private MapsTable() {
        batch("SELECT * FROM `mapids`");
    }

    @Override
    public void fillTable(final ResultSet rs) throws SQLException {
        final PerformanceTimer timer = new PerformanceTimer();
        final StringBuilder sb = new StringBuilder();
        sb.append("加载数据库：地图");

        while (rs.next()) {
            L1Maps map = new L1Maps();
            map.setMapId(rs.getInt("map_id"));
            map.setMapName(rs.getString("map_name"));
            map.setStartX(rs.getInt("start_x"));
            map.setEndX(rs.getInt("end_x"));
            map.setStartY(rs.getInt("start_y"));
            map.setEndY(rs.getInt("end_y"));
            list.put(Integer.valueOf(map.getMapId()), map);
        }

        sb.append("(");
        sb.append(list.size());
        sb.append(")\t耗时 ");
        sb.append(timer.elapsedTimeMillis());
        sb.append(" 毫秒");
        Util.message(Util.SYSTEM_MESSAGE, sb.toString());
    }

    /**
     * 取得缓存列表.
     * 
     * @return 列表中的所有元素
     */
    public Map<Integer, L1Maps> get() {
        return list;
    }

    /**
     * 取得缓存列表.
     * 
     * @return 列表中的所有元素
     */
    public Collection<L1Maps> getList() {
        return list.values();
    }

    /**
     * 根据地图编号取得L1Maps对象.
     * 
     * @param mapId
     *            - 地图编号
     * @return L1Maps对象(如果没找到则返回null)
     */
    public L1Maps getMap(final int mapId) {
        if (list.containsKey(mapId)) {
            return list.get(mapId);
        }
        return null;
    }
}
